package org.kiwi.data.util;

import org.kiwi.data.annotation.DbShardingStrategyName;
import org.kiwi.data.annotation.TableShardingStrategyName;
import org.kiwi.data.sharding.ClassPathShardingStrategyScanner;
import org.kiwi.data.vo.TableShardingParameter;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.util.Assert;

import java.lang.annotation.Annotation;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by jack on 16/12/20.
 */
public class ShardingUtil {

    public static ManagedMap<String, BeanDefinitionHolder> scan(ClassPathShardingStrategyScanner classPathShardingStrategyScanner,String shardingStrategyPackage) {

        ManagedMap<String, BeanDefinitionHolder> strategyMap = new ManagedMap<>();

        Set<BeanDefinitionHolder> strategySet = classPathShardingStrategyScanner.doScan(shardingStrategyPackage);

        for (Iterator<BeanDefinitionHolder> iterator = strategySet.iterator(); iterator.hasNext(); ) {
            BeanDefinitionHolder beanDefinitionHolder = iterator.next();
            Class<?> clazz;
            try {
                BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition();
                clazz = Thread.currentThread().getContextClassLoader().loadClass(beanDefinition.getBeanClassName());
            } catch (ClassNotFoundException e) {
                continue;
            }
            if (clazz != null) {
                Annotation annotation = clazz.getAnnotation(classPathShardingStrategyScanner.getShardingStrategyClass());
                if (annotation != null) {
                    try {
                        if (DbShardingStrategyName.class.equals(classPathShardingStrategyScanner.getShardingStrategyClass())) {
                            strategyMap.put(((DbShardingStrategyName) annotation).value(), beanDefinitionHolder);
                        } else if (TableShardingStrategyName.class.equals(classPathShardingStrategyScanner.getShardingStrategyClass())) {
                            strategyMap.put(((TableShardingStrategyName) annotation).value(), beanDefinitionHolder);
                        } else {
                            throw new IllegalArgumentException("invalid sharding strategy annotation");
                        }
                    } catch (Exception e) {
                        throw new IllegalArgumentException(e.getMessage());
                    }
                }
            }
        }

        return strategyMap;
    }

    public static String convertSql(Object rawSql, TableShardingParameter tableShardingParameter) {
        Assert.notNull(rawSql, "rawSql must not be null");

        return tableShardingParameter.getTableShardingStrategy().determineCurrentSql(
                rawSql.toString(),
                tableShardingParameter.getStrategyName(),
                tableShardingParameter.getShardingParameter()
        );
    }

    public static String replaceAll(String tableName, String realTableName, String sql) {
        Assert.hasText(tableName);
        Assert.hasText(realTableName);
        Assert.hasText(sql);

        return sql.replaceAll(getPlaceholderTableName(tableName), realTableName);
    }

    public static String getPlaceholderTableName(String tableName) {
        Assert.hasText(tableName);

        return "\\$\\[" + tableName + "\\]\\$";
    }

}
